package com.bw.shopping.mvvm.viewmodel;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.bw.greendao.dao.DaoSessionUtils;
import com.bw.greendao.entity.UserEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm_common.utils.IntentAction;
import com.bw.shopping.R;
import com.bw.shopping.adapter.AdressAdapter;
import com.bw.shopping.entity.AdressEntity;
import com.bw.shopping.entity.DeleteAdressEntity;
import com.bw.shopping.mvvm.model.AdressModel;
import com.bw.shopping.mvvm.view.AddAdressActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/29 13:38
 */
public class AdressViewModel extends BaseViewModel<AdressModel> implements OnItemChildClickListener {
    AdressEntity adressEntity;
    public AdressAdapter adapter = new AdressAdapter();

    @Override
    public void oncreate() {
        super.oncreate();
        EventBus.getDefault().register(this);
        adapter.setOnItemChildClickListener(this);
        //请求地址列表
        HashMap<String, Object> map = new HashMap<>();
        List<UserEntity> list = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();
        map.put("id",list.get(0).getUser_id());
        m.requestAdressList(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    //添加地址
    public void addAdressClick(){
        IntentAction intentAction = new IntentAction();
        intentAction.ation = "AddAdress";
        intentAction.flag = true;
        EventBus.getDefault().postSticky(intentAction);
        HashMap<String, Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, AddAdressActivity.class);
        startPage(map);
        finishPage();

    }

    public void back(){
        finishPage();
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity instanceof AdressEntity){
            adressEntity = (AdressEntity) baseEntity;
            adapter.setNewInstance(adressEntity.getData());
        }
        if(baseEntity instanceof DeleteAdressEntity){
            showMsg("删除成功");
            //请求地址列表
            HashMap<String, Object> map = new HashMap<>();
            List<UserEntity> listt = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();
            map.put("id",listt.get(0).getUser_id());
            m.requestAdressList(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        }
    }

    @Override
    public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {

        if(R.id.item_adress_radio == view.getId()){
            IntentAction intentAction = new IntentAction();
            intentAction.ation = "sendAdress";
            intentAction.username = adressEntity.getData().get(position).getShip_user_name();
            intentAction.adress = adressEntity.getData().get(position).getShip_address();
            intentAction.mobile = adressEntity.getData().get(position).getShip_user_mobile();
            intentAction.id = adressEntity.getData().get(position).getId();
            EventBus.getDefault().postSticky(intentAction);
            finishPage();
        }else if(R.id.item_adress_update == view.getId()){
            IntentAction intentAction = new IntentAction();
            intentAction.ation = "updateAdress";
            intentAction.flag = false;
            intentAction.username = adressEntity.getData().get(position).getShip_user_name();
            intentAction.adress = adressEntity.getData().get(position).getShip_address();
            intentAction.id = adressEntity.getData().get(position).getId();
            EventBus.getDefault().postSticky(intentAction);
            HashMap<String, Object> map = new HashMap<>();
            map.put(UIField.PAGECLAZZ,AddAdressActivity.class);
            startPage(map);
            finishPage();
        }else if(R.id.item_adress_del == view.getId()){
            List<UserEntity> list = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id",adressEntity.getData().get(position).getId());
            hashMap.put("userId",list.get(0).getUser_id());
            m.requestDeleteAdress(hashMap)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);


        }

    }

    @Subscribe(sticky = true)
    public void backAdress(IntentAction intentAction){
        if(intentAction.ation.equals("return")){
            showMsg("aaaaaaaaaaa");
            //请求地址列表
            HashMap<String, Object> map = new HashMap<>();
            List<UserEntity> list = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();
            map.put("id",list.get(0).getUser_id());
            m.requestAdressList(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        }
    }

    @Override
    public void onDestory() {
        super.onDestory();
        EventBus.getDefault().unregister(this);
    }
}
